package com.zmc.order.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.order.infrastructure.bean.query.CommentInfoQuery;
import com.zmc.order.infrastructure.bean.query.GoodDetailCommentQuery;
import com.zmc.order.infrastructure.bean.vo.CommentInfoVo;
import com.zmc.order.infrastructure.entity.CommentMemberInfoEntity;
import com.zmc.order.infrastructure.enums.CommentEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.order.infrastructure.dao.CommentInfoDao;
import com.zmc.order.infrastructure.entity.CommentInfoEntity;
import com.zmc.order.infrastructure.service.CommentInfoService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("commentInfoService")
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoDao, CommentInfoEntity> implements CommentInfoService {

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public PageUtils queryPage(CommentInfoQuery query) {
        return new PageUtils(baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query)));
    }

    @Override
    @Transactional
    public CommentInfoEntity initCommentInfo(CommentMemberInfoEntity commentMemberInfo, CommentInfoVo vo) {
        CommentInfoEntity entity = new CommentInfoEntity();
        entity.setOrderSn(commentMemberInfo.getOrderSn());
        entity.setSpuId(commentMemberInfo.getSpuId());
        entity.setMemberId(commentMemberInfo.getMemberId());
        entity.setUsername(authTokenService.getMemberInfo().getNickname());
        entity.setIsShowPictures(CommentEnum.IS_NOT_SHOW_PICTURE.getStatus());
        entity.setIsAppendComment(CommentEnum.IS_NOT_APPEND_COMMENT.getStatus());
        entity.setIsMerchantReply(CommentEnum.IS_MERCHANT_NOT_RELY.getStatus());
        entity.setIsDefaultComment(CommentEnum.IS_DEFAULT_COMMENT.getStatus());
        entity.setIsShowPictures(CommentEnum.IS_NOT_SHOW_PICTURE.getStatus());
        entity.setIsAppendComment(CommentEnum.IS_NOT_APPEND_COMMENT.getStatus());
        entity.setIsMerchantReply(CommentEnum.IS_MERCHANT_NOT_RELY.getStatus());
        entity.setIsDefaultComment(CommentEnum.IS_DEFAULT_COMMENT.getStatus());
        entity.setLogisticsScore(vo.getLogisticsScore());
        entity.setGoodsScore(vo.getGoodScore());
        int totalScore = (vo.getGoodScore() + vo.getLogisticsScore()) / 2;
        entity.setTotalScore(totalScore);
        entity.setCommentContent(vo.getCommentContent());
        entity.setCommentType(totalScore >= 4 ? 1 : totalScore == 3 ? 2 : 3);
        entity.setIsDefaultComment(CommentEnum.IS_USER_COMMENT.getStatus());
        save(entity);
        return entity;
    }

    @Override
    public PageUtils queryGoodDetailCommentPage(GoodDetailCommentQuery query) {
        LambdaQueryWrapper<CommentInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentInfoEntity::getSpuId, query.getSpuId())
                .eq(query.getType() == 1, CommentInfoEntity::getIsShowPictures, CommentEnum.IS_SHOW_PICTURE.getStatus())
                .eq(query.getType() == 2, CommentInfoEntity::getIsAppendComment, CommentEnum.IS_APPEND_COMMENT.getStatus())
                .eq(query.getType() == 3, CommentInfoEntity::getCommentType, CommentEnum.COMMENT_RESULT_GOOD.getStatus())
                .eq(query.getType() == 4, CommentInfoEntity::getCommentType, CommentEnum.COMMENT_RESULT_MID.getStatus())
                .eq(query.getType() == 5, CommentInfoEntity::getCommentType, CommentEnum.COMMENT_RESULT_BAD.getStatus());
        return new PageUtils(baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), wrapper));
    }

    @Override
    public CommentInfoEntity getInfo(String spuId, String orderSn) {
        LambdaQueryWrapper<CommentInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentInfoEntity::getSpuId, spuId)
                .eq(CommentInfoEntity::getOrderSn, orderSn)
                .last(" limit 1");
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void updateAppendComment(String appendContent, int appendStatus, String spuId, String orderSn) {
        LambdaUpdateWrapper<CommentInfoEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CommentInfoEntity::getSpuId, spuId)
                .eq(CommentInfoEntity::getOrderSn, orderSn)
                .set(CommentInfoEntity::getAppendComment, appendContent)
                .set(CommentInfoEntity::getIsAppendComment, appendContent);
        update(wrapper);

    }

    @Override
    public void updateMerchantApply(String spuId, String orderSn, Integer answerStatus, String answer) {
        LambdaUpdateWrapper<CommentInfoEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CommentInfoEntity::getSpuId, spuId)
                .eq(CommentInfoEntity::getOrderSn, orderSn)
                .set(CommentInfoEntity::getReplyComment, answer)
                .set(CommentInfoEntity::getIsMerchantReply, answerStatus);
        update(wrapper);
    }

    public LambdaQueryWrapper<CommentInfoEntity> getWrapper(CommentInfoQuery query) {
        LambdaQueryWrapper<CommentInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommentInfoEntity::getSpuId, query.getSpuId())
                .eq(!Objects.isNull(query.getIsAppendComment()), CommentInfoEntity::getIsAppendComment, query.getIsAppendComment())
                .eq(!Objects.isNull(query.getIsMerchantReply()), CommentInfoEntity::getIsAppendComment, query.getIsMerchantReply());
        return wrapper;
    }

}